<!-- 设备动作的联动视频 -->

<template>
  <div>
    <FormSlot v-if="one.actionVideoPop === 1" label="联动视频" bind="actionVideoDeviceId" required>
      <span v-if="one.actionVideoDeviceId" class="el-button--text" @click="toOpen">
        <span>{{ one.actionVideoDeviceName }}</span>
      </span>

      <span v-if="!isShow">
        <BtnText v-if="one.actionVideoDeviceId" title="清除" @click="toClear" />
        <BtnPrimary v-else title="选择" @click="toOpen" />
      </span>
    </FormSlot>

    <EasyModal v-model="easyVisible" title="选择视频" medium @submit="toSubmit" @close="toReset">
      <ChooseIOTOne :obj="obj" bind="device" deviceCategoryId="1" disableCategory />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'ActionVideo',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { device: {} }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this
      this.easyVisible = false

      const { id, deviceName } = obj.device
      CORE.merge(one, {
        actionVideoDeviceId: id,
        actionVideoDeviceName: deviceName,
      })
    },

    /**
     * 重置
     */
    toReset() {
      const { one } = this
      const device = {
        id: one.actionVideoDeviceId,
        deviceName: one.actionVideoDeviceName,
      }
      this.obj = { device }
    },

    /**
     * 清除
     */
    toClear() {
      CORE.merge(this.one, {
        actionVideoDeviceId: undefined,
        actionVideoDeviceName: undefined,
      })
      this.obj = { device: {} }
    },
  },
}
</script>
